/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.logist;

import dp1.titandevelop.titano.bean.Estado;
import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Cotizacion;
import dp1.titandevelop.titano.persistent.OrdenCompra;
import dp1.titandevelop.titano.service.CotizacionService;
import dp1.titandevelop.titano.service.OrdenCompraService;
import java.awt.Dimension;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author chrestean
 */
public class ModulosT_LOG_EditarOrdCompra extends javax.swing.JInternalFrame {

    private Cotizacion cotizacion;
    private ModulosT_LOG_AdministrarOrdenCompra caller;

    /**
     * Creates new form TitanoInternalSegADMUserNew
     */
    public ModulosT_LOG_EditarOrdCompra(ModulosT_LOG_AdministrarOrdenCompra caller) {
        initComponents();


        //this.cmbEstado.addItem(Estado.devuelveCadena(0));
        this.cmbEstado.addItem(Estado.devuelveCadena(1));
        this.cmbEstado.addItem(Estado.devuelveCadena(2));
        this.cmbEstado.addItem(Estado.devuelveCadena(3));
        this.cmbEstado.addItem(Estado.devuelveCadena(4));
        this.caller = caller;
        this.datePkFecha.setDate(new Date());
        this.nombre.setVisible(false);
        this.txtIDCotizacion.setVisible(false);
        this.jButton2.setVisible(false);
        this.jButton3.setVisible(false);

    }
    private OrdenCompra ordenCompra = null;

    ModulosT_LOG_EditarOrdCompra(ModulosT_LOG_AdministrarOrdenCompra caller, OrdenCompra oc) {
        initComponents();
        this.setTitle("Editar Orden de Compra");
        this.ordenCompra = oc;
        this.cmbEstado.addItem(Estado.devuelveCadena(1));
        this.cmbEstado.addItem(Estado.devuelveCadena(2));
        this.cmbEstado.addItem(Estado.devuelveCadena(3));
        this.cmbEstado.addItem(Estado.devuelveCadena(4));
        this.caller = caller;
        this.datePkFecha.setDate(oc.getFecharegistro());
        this.datePkFechaEntrega.setDate(oc.getFechaentrega());
//        this.txtIDCotizacion.setText("" + oc.getCotizacionArray().get(0).getIdcotizacion());
        this.cmbEstado.setSelectedItem(oc.getEstado());
        this.txtIDCotizacion.setEnabled(false);

        this.nombre.setVisible(false);
        this.txtIDCotizacion.setVisible(false);
        this.jButton2.setVisible(false);
        this.jButton3.setVisible(false);

    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        nombre = new javax.swing.JLabel();
        apPaterno = new javax.swing.JLabel();
        fechaNac = new javax.swing.JLabel();
        telefono = new javax.swing.JLabel();
        txtIDCotizacion = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        datePkFecha = new org.jdesktop.swingx.JXDatePicker();
        datePkFechaEntrega = new org.jdesktop.swingx.JXDatePicker();
        cmbEstado = new javax.swing.JComboBox();
        btnGuardar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setTitle("Nueva Orden de Compra");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Orden Compra", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        nombre.setText("ID Cotizacion");

        apPaterno.setText("Fecha");

        fechaNac.setText("Fecha Entrega");

        telefono.setText("Estado");

        txtIDCotizacion.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/detalle.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar2.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        datePkFecha.setEditable(false);
        datePkFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                datePkFechaActionPerformed(evt);
            }
        });

        cmbEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbEstadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombre)
                            .addComponent(apPaterno))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(datePkFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(23, 23, 23))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtIDCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(telefono)
                                .addGap(37, 37, 37))
                            .addComponent(fechaNac, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(datePkFechaEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nombre)
                        .addComponent(txtIDCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton3))
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(apPaterno)
                    .addComponent(datePkFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(datePkFechaEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fechaNac))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(telefono))
                .addGap(8, 8, 8))
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        btnGuardar.setToolTipText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnSalir.setToolTipText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnGuardar)
                        .addGap(1, 1, 1)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(8, 8, 8))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnGuardar)
                    .addComponent(btnSalir))
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed

        ModulosT_LOG_AdministrarOrdenCompra admOrdCompras = new ModulosT_LOG_AdministrarOrdenCompra();
        this.getParent().add(admOrdCompras);

        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = admOrdCompras.getSize();
        admOrdCompras.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height) / 4);

        admOrdCompras.show();
        this.dispose();

    }//GEN-LAST:event_btnSalirActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        CotizacionService cs = new CotizacionService();
        cotizacion = cs.buscarPorId(null, Integer.parseInt(this.txtIDCotizacion.getText()));
        if (cotizacion.getToOrdenCompra().getIdordencompra() == 0) {
            this.cmbEstado.setSelectedIndex(cotizacion.getEstado());
            this.datePkFecha.setDate(cotizacion.getFecharegistro());
        } else {
            JOptionPane.showMessageDialog(this, "La cotizacion ya fue asignada a una orden de compra", "Asignacion", JOptionPane.ERROR_MESSAGE);
            this.cmbEstado.setSelectedIndex(0);
            this.cotizacion = null;
            this.txtIDCotizacion.setText("");
        }


    }//GEN-LAST:event_jButton2ActionPerformed

    private void datePkFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_datePkFechaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_datePkFechaActionPerformed

    private void cmbEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbEstadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbEstadoActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        Mensajes m = new Mensajes();
        if (this.ordenCompra == null) {
//            CotizacionService cs = new CotizacionService();
//            OrdenCompraService ocs = new OrdenCompraService();
////            OrdenCompra oc = ocs.insertarOrdenCompra(this.cotizacion, this.cotizacion.getFecharegistro(), this.datePkFechaEntrega.getDate(), Estado.getEstadoId(this.cmbEstado.getSelectedItem().toString()));
//            this.cotizacion.setToOrdenCompra(oc);
//            cs.actualizarCotizacion(this.cotizacion);
//            this.caller.show();
//            this.dispose();
        } else {
            try{
            this.caller.hide();
            this.ordenCompra.setEstado(Estado.getEstadoId(cmbEstado.getSelectedItem().toString()));
            this.ordenCompra.setFechaentrega(this.datePkFechaEntrega.getDate());
            this.ordenCompra.setFecharegistro(this.datePkFecha.getDate());
            this.ordenCompra.getObjectContext().commitChanges();
             JOptionPane.showInternalMessageDialog(this.getRootPane(), m.getEditado(),"Mensaje",JOptionPane.INFORMATION_MESSAGE);
            this.caller.show();
            this.dispose();

           } catch(Exception e) {
                  JOptionPane.showInternalMessageDialog(this.getRootPane(),m.getError(),"Error",JOptionPane.ERROR_MESSAGE);

           }


        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    public void setIdCotizacion(int id) {
        this.txtIDCotizacion.setText("" + id);
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        ModulosT_LOG_BuscarCotizacion bc = new ModulosT_LOG_BuscarCotizacion(this);
        this.getParent().add(bc);
        bc.show();
    }//GEN-LAST:event_jButton3ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel apPaterno;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbEstado;
    private org.jdesktop.swingx.JXDatePicker datePkFecha;
    private org.jdesktop.swingx.JXDatePicker datePkFechaEntrega;
    private javax.swing.JLabel fechaNac;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel nombre;
    private javax.swing.JLabel telefono;
    private javax.swing.JTextField txtIDCotizacion;
    // End of variables declaration//GEN-END:variables
}
